import fastapi
import uvicorn
from opentelemetry import trace
from opentelemetry.exporter.jaeger.thrift import JaegerExporter
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
from opentelemetry.sdk.resources import SERVICE_NAME, Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor

# Set up the tracer provider
trace.set_tracer_provider(
    TracerProvider(resource=Resource.create({SERVICE_NAME: 'Fasttapi-users'})))

tracer_provider = trace.get_tracer_provider()
# Configure Jaeger exporter
jaeger_exporter = JaegerExporter(
    agent_host_name='localhost',
    agent_port=6831,
)
# Add the Jaeger exporter to the tracer provider
tracer_provider.add_span_processor(BatchSpanProcessor(jaeger_exporter))

app = fastapi.FastAPI()


@app.get('/foobar')
async def foobar():
    return {'message': 'hello world'}


FastAPIInstrumentor.instrument_app(app)

if __name__ == '__main__':
    # https://www.jaegertracing.io/docs/1.59/getting-started/
    uvicorn.run(app, host='0.0.0.0', port=8000)
